package com.csproj.basic.repository;

import com.csproj.basic.domain.PDA;
import com.csproj.basic.repository.custom.PDARepositoryCustom;
import org.springframework.stereotype.Repository;

import org.springframework.data.jpa.repository.*;

import java.util.List;


/**
 * Spring Data JPA repository for the PDA entity.
 */
@SuppressWarnings("unused")
@Repository
public interface PDARepository extends JpaRepository<PDA, String>, PDARepositoryCustom {
    /**
     * 根据ids查询PDA列表信息
     * @author LM
     * @date   2018-09-14
     * @param ids
     * @return
     */
    @Query(" from PDA where deletedFlag = 0 and id in(?1) ")
    List<PDA> findByIds(String[] ids);


    /**
     * 根据ID删除数据
     * @author LM
     * @date   2018-09-14
     * @param id 设备id
     */
    @Modifying
    @Query("UPDATE PDA SET status = ?2 where id = ?1")
    void updateStatus(String id, Integer status);

    /**
     * 根据ID删除数据
     * @author LM
     * @date   2018-09-14
     * @param id 机构id
     */
    @Query(" from PDA where deletedFlag = 0 and id = ?1")
    PDA findById(String id);

    /**
     * 根据机构ids，批量pda设备信息
     * @param ids
     */
    @Modifying
    @Query(" update PDA set deletedFlag = 1 where id in (?1) ")
    void deleteIds(String[] ids);

    @Query(" From PDA  where siteId = '' or siteId is null and  deletedFlag = 0 ")
    List<PDA> findBySiteIdSotck();

    /**
     * @param  deviceType 设备类型
     * 查询未归还数量
     * @return
     */
    @Query( " select pda FROM CollarRecord cr left join  PDA  pda on cr.deviceId = pda.id where cr.deviceType = ?1 and cr.backDate is null and  pda.deletedFlag = 0 ")
    List<PDA> findByCollarRecordUnBackDate(Integer deviceType);

    PDA findFirstByPdaNoAndDeletedFlagIsFalse(String pdaNo);

    @Query(" from PDA where siteId = ?1 and enableFlag = ?2 and  deletedFlag = 0 ")
    List<PDA> findbySiteIdAndEnableFlag(String siteId,Boolean state);

    @Query( " select pda FROM CollarRecord cr left join  PDA  pda on cr.deviceId = pda.id where cr.deviceType = ?1 and pda.siteId= ?2 and pda.enableFlag= true and cr.backDate is null and  pda.deletedFlag = 0  ")
    List<PDA> findByCollarRecordUnBackDateAndSiteIdAAndEnableFlag(Integer deviceTypeEle, String siteId);
}
